package com.zenitera.bigdata.state;

import com.zenitera.bigdata.bean.WaterSensor;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * ReducingState
 */
public class Flink02_State_Key_ReduceState {
    public static void main(String[] args) {

        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env
                .socketTextStream("localhost", 6666)
                .map(line -> {
                    String[] data = line.split(",");
                    return new WaterSensor(
                            String.valueOf(data[0]),
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2])
                    );
                })
                .keyBy(WaterSensor::getId)
                .process(new KeyedProcessFunction<String, WaterSensor, String>() {


                    private ReducingState<WaterSensor> vcSumState;

                    @Override
                    public void open(Configuration parameters) {
                        vcSumState = getRuntimeContext().getReducingState(
                                new ReducingStateDescriptor<>(
                                        "vcSumState",
                                        (ReduceFunction<WaterSensor>) (value1, value2) -> {
                                            value1.setVc(value1.getVc() + value2.getVc());
                                            System.out.println(value1.getVc());
                                            return value1;
                                        },
                                        WaterSensor.class
                                ));
                    }

                    @Override
                    public void processElement(WaterSensor value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        vcSumState.add(value);

                        out.collect(vcSumState.get().toString());

                    }
                })
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
/*
D:\netcat-win32-1.12>nc64.exe -lp 6666
s1,1,1
s1,1,3
s1,1,5
s1,1,10
--------------------------
com.zenitera.bigdata.bean.WaterSensor@80c7dff
4
com.zenitera.bigdata.bean.WaterSensor@80c7dff
9
com.zenitera.bigdata.bean.WaterSensor@80c7dff
19
com.zenitera.bigdata.bean.WaterSensor@80c7dff
 */